<?php
include "header.php";
include "mysqli_connection.php";

if (!isset($_SESSION["username"])){
	echo "<script>window.location.href='login.php'</script>";
}
else{
?>
<script>change_picture("admin");</script>
<script>
function jump(){
	window.location.href='manageprograms.php';
}
window.setTimeout("jump();",3000);
</script>
<div>
<?php
	if (isset($_POST["p_id"]) && $_POST["p_id"] !== ""){
		$p_id = $mysqli->real_escape_string(trim($_POST["p_id"]));
		if ($p_id == 0){
			$query = "INSERT INTO Programs (major, entry_year, starting_course) VALUES (?, ?, ?);";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("sdi", trim($_POST["major"]), trim($_POST["entry_year"]), trim($_POST["starting_course"]));
				if (!$stmt->execute()){
					$stmt->close();
					echo "Insert failed! ";
				}
				else{
					$stmt->close();
					$query = "SELECT p_id FROM Programs WHERE (major = ?) AND (entry_year = ?) AND (starting_course = ?);";
					$stmt = $mysqli->stmt_init();
					if ($stmt = $mysqli->prepare($query)){
						$stmt->bind_param("sdi", trim($_POST["major"]), trim($_POST["entry_year"]), trim($_POST["starting_course"]));
						$stmt->execute();
						$stmt->bind_result($p_id);
						if ($stmt->fetch()){
							$stmt->close();
							if ($_POST["requirement_list"] !== ""){
								$requirement_list = explode(",", $_POST["requirement_list"]);
								$ok = true;
								$query = "INSERT INTO Requirement_Program (r_id, p_id) VALUES (?, ?);";
								$stmt = $mysqli->stmt_init();
								if ($stmt = $mysqli->prepare($query)){
									$stmt->bind_param("ii", $item, $p_id);
									foreach ($requirement_list as $item){
										if (!$stmt->execute()){
											$ok = false;
										}
									}
								}
								else{
									$ok = false;
								}
								$stmt->close();
								if (!$ok){
									echo "Update failed! ";
								}
								else{
									echo "Update Succeed! ";
								}
							}
							else{
								echo "Update Succeed! ";
							}
						}
						else{
							$stmt->close();
							echo "Insert failed! ";
						}
					}
				}
			}
		}
		else{
			$query = "UPDATE Programs SET major = ?, entry_year =?, starting_course = ? WHERE p_id = ?;";
			$stmt = $mysqli->stmt_init();
			if ($stmt = $mysqli->prepare($query)){
				$stmt->bind_param("sdii", trim($_POST["major"]), trim($_POST["entry_year"]), trim($_POST["starting_course"]), $p_id);
				if (!$stmt->execute()){
					$stmt->close();
					echo "Update failed! ";
				}
				else{
					$stmt->close();
					$query = "DELETE FROM Requirement_Program WHERE p_id = ?;";
					$stmt = $mysqli->stmt_init();
					if ($stmt = $mysqli->prepare($query)){
						$stmt->bind_param("i", $p_id);
						if (!$stmt->execute()){
							$stmt->close();
							echo "Update failed! ";
						}
						else{
							$stmt->close();
							if ($_POST["requirement_list"] !== ""){
								$requirement_list = explode(",", $_POST["requirement_list"]);
								$ok = true;
								$query = "INSERT INTO Requirement_Program (r_id, p_id) VALUES (?, ?);";
								$stmt = $mysqli->stmt_init();
								if ($stmt = $mysqli->prepare($query)){
									$stmt->bind_param("ii", $item, $p_id);
									foreach ($requirement_list as $item){
										if (!$stmt->execute()){
											$ok = false;
										}
									}
								}
								else{
									$ok = false;
								}
								$stmt->close();
								if (!$ok){
									echo "Update failed! ";
								}
								else{
									echo "Update Succeed! ";
								}
							}
							else{
								echo "Update Succeed! ";
							}
						}
					}
				}
			}
		}
	}
?>
Jumping to manage courses page in 3 seconds...<br />
<a href="manageprograms.php">Click here to jump immediately</a>
</div>

<?php
}
include "footer.php";